/**
 * Created by apple on 17/3/13.
 */

var express = require('express');
var router = express.Router();

var db = require('../db')
var dbHelper = require('../db/helper')
var helper = new dbHelper.helper();

var DbUtils = require('../db/index2')

/* GET users listing. */
router.get('/', function (req, res, next) {
    var page = db.page;
    page.pageNo = 2;
    db.query('select * from medicines limit 1,50', [], function (data) {
        //res.json(data);
        res.setHeader("x-total-count",50)
        res.render('medicines', { data: data });
    })

});

/* GET medicines pagination */
router.get('/page.json', function (req, res, next) {
    console.log(req.params)

    console.log(req.query)
    console.log(req.body)
    var page = db.page;
    page.pageNo = req.query.pageNo || page.pageNo;
    page.pageSize =  req.query.pageSize ||page.pageSize ;
    db.pagination('select * from medicines', [], page, function (data) {

        res.json(data);
      //  res.render('medicines', { data: data });
    })

});

/* GET users listing. */
router.get('/:id', function (req, res, next) {

    req.params.id > 50 ? res.send("示例数据只能查看50条")
        :
        db.query('select * from medicines where id =?', req.params.id, function (data) {
            res.json(data)
        });

});

/* GET medicines pagination */
router.get('/page/show', function (req, res, next) {
    console.log("showxxxx")
    console.log(req.query)
    console.log(req.body)
    var page = db.page;
    page.pageNo = req.query.pageNo || page.pageNo;
    page.pageSize =  req.query.pageSize ||page.pageSize ;
    console.log(page)
    db.pagination('select * from medicines', [], page, function (data) {
        res.render('medicines', { data: data });
        //res.json(data);

    })

});

router.get('/page/json',function (req,res,next) {

    let [a,b,c,d,e,f,g,h] = [1,2,3,4,5,7,6,'gggg']

    let arr = [1,2,3,4,5,7,6]
    console.log(arr.sort().reverse())

    console.log(a,b,c,d,e,f,g,h)
    var page = db.page;
    page.current = req.query.current || page.current;
    page.pageSize =  req.query.pageSize ||page.pageSize ;
    console.log(page)

    var pagination = {
        current: page.current, totalPages: 0,pageSize:page.pageSize,total:0
    }

    // let count  = db.total('select count(1) from medicines',[])
    //
    // console.log('count',count)
    console.log(DbUtils)

    let count = DbUtils.totalCount('select count(1) from medicines',[])

    console.log('counxxxxxxxxxt', count)



    db.query('select count(1) count from medicines',[],function (result) {
        console.log(result)
        if (result) {
            pagination.total =  result[0].count;
            pagination.totalPages = Math.ceil(result[0].count / page.pageSize)
        }
        console.log("1",pagination);
        db.pagination('select * from medicines',[],page,function (datas) {
        //    data.datas = datas
            console.log("2",pagination)
            res.setHeader("pagination",JSON.stringify(pagination))
            res.json(datas)
        })
    })

})


module.exports = router;
